package com.my.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.logical.shared.SelectionEvent;
import com.google.gwt.event.logical.shared.SelectionHandler;
import com.google.gwt.json.client.JSONArray;
import com.google.gwt.json.client.JSONObject;
import com.google.gwt.json.client.JSONString;
import com.google.gwt.json.client.JSONValue;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DecoratedTabPanel;
import com.google.gwt.user.client.ui.DecoratorPanel;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.HorizontalSplitPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.SourcesTabEvents;
import com.google.gwt.user.client.ui.TabListener;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Tree;
import com.google.gwt.user.client.ui.TreeItem;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.user.client.ui.FlexTable.FlexCellFormatter;
import com.my.client.i18n.MyMessage;
import com.my.client.util.FormMap;
import com.my.client.util.ResultMap;
import com.my.client.util.StaticService;
import com.my.client.util.UIRender;
import com.my.client.util.Zend;

public class IndexView implements EntryPoint {
	public static final int ACT_TREE = 1;
	public static final int ACT_SEARCH = ACT_TREE + 1;
	private final GreetingServiceAsync greetingService = GWT
			.create(GreetingService.class);
	private final MyMessage message = GWT.create(MyMessage.class);

	public void onModuleLoad() {
		// TODO Auto-generated method stub
		StaticService.getInstance().setService(greetingService);
		RootPanel.get().add(onInitialize());
	}

	private Button searchBtn = null;
	private TextBox searchBox = null;

	private Widget onInitialize() {
		VerticalPanel vp = new VerticalPanel();
		vp.setSpacing(3);
		//search
		HorizontalPanel hp = new HorizontalPanel();
		hp.setSpacing(2);
		searchBtn = new Button();
		searchBtn.setText(" search");
		searchBox = new TextBox();
		searchBtn.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				handler(ACT_SEARCH);
			}
		});
		hp.add(searchBox);
		vp.add(searchBtn);
		vp.add(hp);

		HorizontalPanel leftPanel = new HorizontalPanel();
		leftPanel.setSpacing(2);
		//tab
		String[] tabTitles = new String[] { message.tab_home(),
				message.tab_panhang(), message.tab_my() };
		//create home 
		HorizontalPanel tabHome = new HorizontalPanel();
		tabHome.setSpacing(2);
		tabHome.add(createTab_home());
		//careat panhang
		HTML moreInfo = new HTML("constants.中文cwTabPanelTab2()");
		//create pro
		VerticalPanel vPanel = new VerticalPanel();
		vPanel.add(new Image("images/logo.gif"));
		Widget[] widgets = { tabHome, moreInfo, vPanel };
		Widget tabs = onTab(tabTitles, widgets);
		leftPanel.add(tabs);
		leftPanel.add(onLogin());

		vp.add(leftPanel);
		//fooot
		vp.add(new HTML("foot............"));
		//tab 
		//tree
		return vp;
	}

	private HorizontalSplitPanel hSplit = null;

	private Widget createTab_home() {
		// Create a Horizontal Split Panel
		hSplit = new HorizontalSplitPanel();
		hSplit.ensureDebugId("cwHorizontalSplitPanel");
		hSplit.setSize("500px", "350px");
		hSplit.setSplitPosition("30%");
		//tree
		JSONString js = new JSONString("[" + "{'n':'item1','id':'1','p':["
				+ "{'n':'item11','id':'2'," + "'p':["
				+ "{'n':'item111','id':'3'}," + "{'n':'item112','id':'4'}]},"
				+ "{'n':'item12','id':'5'}" + "]}," + "{'n':'item1','id':'6'},"
				+ "{'n':'item1','id':'7'}," + "]");

		JSONArray ja = new JSONArray();//js.isArray();
		for (int i = 0; i < 3; i++) {
			JSONObject jo = new JSONObject();
			jo.put("n", new JSONString("item" + i));
			jo.put("id", new JSONString("" + i + 1));
			if (i == 1) {
				JSONArray ja_ = new JSONArray();
				for (int j = 0; j < 3; j++) {
					JSONObject jo_ = new JSONObject();
					jo_.put("n", new JSONString("item1" + i));
					jo_.put("id", new JSONString("" + i + 1));
					ja_.set(j, jo_);
				}
				jo.put("p", ja_);
			}
			ja.set(i, jo);
		}
		Tree tree = this.createTree(ja);
		hSplit.setLeftWidget(tree);
		//content
		hSplit.setRightWidget(new HTML("test"));
		DecoratorPanel decPanel = new DecoratorPanel();
		decPanel.setWidget(hSplit);

		return decPanel;

	}

	private void handler(int act) {
		switch (act) {
		case ACT_TREE: {

			break;
		}

		case ACT_SEARCH: {
			FormMap formMap = new FormMap();
			formMap.put("search", searchBox.getText());
			Zend.fly(ACT_SEARCH, formMap, new UIRender() {
				public void render(ResultMap resultMap) {
					System.out.println((String) resultMap.get("result"));
					searchBox.setText((String) resultMap.get("result"));
				}
			});
			;
			break;
		}
		}
	}

	private void refreshContent(int id) {
		//sendRequest(TREE_ACT);
		//hSplit.setRightWidget(w);
	}

	/**
	 * Create a static tree with some data in it.
	 * 
	 * @return the new tree
	 */
	private Tree createTree(JSONArray ja) {

		Tree tree = new Tree();
		TreeItem pitem = tree.addItem("root");
		TreeItem item = null;
		int size = ja.size();
		for (int i = 0; i < size; i++) {
			JSONObject jo = ja.get(i).isObject();
			item = pitem.addItem(jo.get("n").isString().stringValue());
			JSONValue jv = jo.get("p");
			if (i == 0)
				item.setHTML("<a href='#' >test</a>");
			if (jv != null) {
				appendChildItem(item, jv.isArray());
			}
		}

		// Return the tree
		return tree;
	}

	private void appendChildItem(TreeItem pitem, JSONArray ja) {
		int size = ja.size();
		TreeItem item = null;
		for (int i = 0; i < size; i++) {
			JSONObject jo = ja.get(i).isObject();
			item = pitem.addItem(jo.get("n").isString().stringValue());
			JSONValue jv = jo.get("p");
			if (jv != null) {
				appendChildItem(item, jv.isArray());
			}
		}
	}

	private DecoratedTabPanel tabPanel;

	public Widget onTab(String[] tabTitles, Widget[] widgets) {
		// Create a tab panel
		tabPanel = new DecoratedTabPanel();
		tabPanel.setWidth("400px");
		tabPanel.setAnimationEnabled(true);

		// Add a home tab
		tabPanel.add(widgets[0], tabTitles[0]);

		// Add a tab with an image
		tabPanel.add(widgets[1], tabTitles[1]);

		// Add a tab
		tabPanel.add(widgets[2], tabTitles[2]);

		// Return the content
		tabPanel.selectTab(0);
		tabPanel.ensureDebugId("cwTabPanel");
		tabPanel.addSelectionHandler(new SelectionHandler() {

			public void onSelection(SelectionEvent event) {
				// TODO Auto-generated method stub
				System.out.println(event.getSelectedItem().getClass() + " ,"
						+ event.getSelectedItem());

			}
		});
		return tabPanel;
	}

	public Widget onLogin() {
		// Create a table to layout the form options
		FlexTable layout = new FlexTable();
		layout.setCellSpacing(6);
		FlexCellFormatter cellFormatter = layout.getFlexCellFormatter();

		// Add a title to the form
		layout.setHTML(0, 0, message.panel_login());
		cellFormatter.setColSpan(0, 0, 2);
		cellFormatter.setHorizontalAlignment(0, 0,
				HasHorizontalAlignment.ALIGN_CENTER);

		// Add some standard form options
		layout.setHTML(1, 0, message.text_uname());
		layout.setWidget(1, 1, new TextBox());
		layout.setHTML(2, 0, message.text_pwd());
		layout.setWidget(2, 1, new TextBox());

		// Wrap the content in a DecoratorPanel
		DecoratorPanel decPanel = new DecoratorPanel();
		decPanel.setWidget(layout);
		return decPanel;
	}

}
